#include "GRE.h"

GRE::GRE()
{}

int GRE::parse(Packet* pkt)
{
	if(sizeof(GRE_Header) >= pkt->pktLength - pkt->parsePos)
	{
		pkt->state = STATE_FAIL;
		return -1;
	}

	uint8_t* data = pkt->rawPkt + pkt->parsePos;

	GRE_Header* header = (GRE_Header*)data;
	pkt->parsePos += 4;

	//校验和 + 偏离
	if(0 != (header->flags & 0x80) || 0 != (header->flags & 0x40))
	{
		pkt->parsePos += 4;
	}
	//秘钥
	if(0 != (header->flags & 0x20))
	{
		pkt->parsePos += 4;
	}
	//序列号
	if(0 != (header->flags & 0x10))
	{
		pkt->parsePos += 4;
	}
	//路由
	if(0 != (header->flags & 0x40))
	{
		pkt->parsePos += 4;
	}

	if(2048 == ntohs(header->protocol))
	{
		pkt->state = STATE_IP;
	}
	else
	{
		pkt->state = STATE_FAIL;
	}

	return 0;
}
